package utils

import (
	"fmt"
	"net"
)

// GetLocalIP 获取本地内网ip
func GetLocalIP() (string, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return "", fmt.Errorf("localIP:fail to getlocal ip,err:%s", err.Error())
	}
	for _, a := range addrs {
		if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				return ipnet.IP.String(), nil

			}
		}
	}
	return "", fmt.Errorf("localIP:fail to getlocal ip,err:%s", err.Error())
}
